package com.party.admin.web.controller.versionMgr;

import com.google.common.base.Strings;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.core.model.version.VersionManager;
import com.party.core.service.version.IVersionManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 第三方应用版本管理
 *
 * @author Juliana
 */
@Controller
@RequestMapping(value = "/system/versionMgr")
public class versionMgrController {

    @Autowired
    private IVersionManagerService versionManagerService;


    protected static Logger logger = LoggerFactory.getLogger(versionMgrController.class);

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping(value = "list")
    public ModelAndView list(VersionManager versionMgr, Page page) {
        ModelAndView mv = new ModelAndView("/system/versionMgr/list");
        List<VersionManager> list = versionManagerService.listPage(versionMgr, page);
        mv.addObject("list", list);
        mv.addObject("page", page);
        return mv;
    }

    /**
     * 详情
     *
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id) {
        ModelAndView modelAndView = new ModelAndView("/system/versionMgr/view");
        VersionManager versionMgr;
        if (Strings.isNullOrEmpty(id)){
            versionMgr = new VersionManager();
        }
        else {
            versionMgr = versionManagerService.get(id);
        }
        modelAndView.addObject("version", versionMgr);
        return modelAndView;
    }

    /**
     * 保存
     *
     * @param versionMgr 系统参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(VersionManager versionMgr) {
        AjaxResult ajaxResult = new AjaxResult();
        if (Strings.isNullOrEmpty(versionMgr.getId())) {
            versionManagerService.insert(versionMgr);
        } else {
            versionManagerService.update(versionMgr);
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 删除
     *
     * @param id 编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public AjaxResult delete(String id) {
        AjaxResult ajaxResult = new AjaxResult();
        versionManagerService.delete(id);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

}
